home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 493 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. Path: solon.com!not-for-mail
  2. From: fred@genesis.demon.co.uk (Lawrence Kirby)
  3. Newsgroups: comp.lang.c.moderated,comp.std.c
  4. Subject: Behaviour of mem functions when n=0 ?
  5. Date: 3 Mar 1996 22:14:29 -0600
  6. Organization: none
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4hdqn5$8qn@solutions.solon.com>
  10. Reply-To: fred@genesis.demon.co.uk
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Mailer: Demon Internet Simple News v1.27
  13.  
  14. In article <4h75af$4ln@solutions.solon.com>
  15.            chrisw@leland.stanford.edu "Chris Wilkins" writes:
  16.  
  17.  > 
  18.  > I was wondering if someone could tell me exactly what behaviour is guaranteed 
  19.  > for the mem library functions when n=0. I presume the answers are:
  20.  > 
  21.  > memcpy()  -> do nothing
  22.  > memmove() -> do nothing
  23.  
  24. The wording in the standard for both of these is 'copies n characters from the
  25. object'. There is nothing to make n==0 illegal so, yes.
  26.  
  27.  > memset()  -> do nothing
  28.  
  29. The standard wording is similar wording so this is correct.
  30.  
  31.  > memcmp()  -> return 0
  32.  
  33. Probably. However the result is defined by some rather unfortunate wording in
  34. the standard:
  35.  
  36. "The memcmp function returns an integer greater than, equal to, or less than
  37.  zero, accordingly as the object pointed to by s1 is greater than, equal to,
  38.  or less than the object pointed to by s2"
  39.  
  40. The problem here is that in the standard the term 'object' means
  41. something with a well-defined non-zero size. An object also has a type.
  42. It doesn't make much sense to describe the action of memcmp as comparing
  43. two objects since that doesn't provide any meaning to the comparison result.
  44. IMHO this is a defect in the standard. I've cross-posted to comp.std.c.
  45.  
  46.  > memchr()  -> return NULL
  47.  
  48. Again, probably. However there is a similar problem here to that with
  49. memcmp.
  50.  
  51. "The memchr function returns a pointer to the located character, or a null
  52.  pointer if the character does not occur in the object"
  53.  
  54. If this had said "... does not occur in the initial n characters of the
  55. object" then fine. However it doesn't say that and you're back to trying
  56. to force the term 'object' to mean something it really can't.
  57.  
  58.  > But are these guaranteed?
  59.  > 
  60.  > Also, when n=0 is it guaranteed that these functions will not dereference 
  61.  > their other arguments? 
  62.  
  63. There is no explicit guarantee.
  64.  
  65.  > Basically, I'm interested whether I can safely use something like:
  66.  > 
  67.  > memcpy(ptr,NULL,0);
  68.  
  69. No. It is explicit that the pointer arguments to these functions have to
  70. point to objects. The functions could legally contain code to the effect of:
  71.  
  72.     if (arg == NULL) abort();
  73.  
  74.  > or whether I have to wrap it:
  75.  > 
  76.  > if(len>0) {
  77.  >     memcpy(dest,src,len);
  78.  > }
  79.  
  80. Yes, assuming that len>0 guarantees that dest and src are legal pointers
  81. to objects of suitable size (e.g. not null pointers).
  82.  
  83. -- 
  84. -----------------------------------------
  85. Lawrence Kirby | fred@genesis.demon.co.uk
  86. Wilts, England | 70734.126@compuserve.com
  87. -----------------------------------------
  88.